package com.jl.book.ui.search

import android.content.Intent
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.room.Room
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jl.book.R
import com.jl.book.base.BaseActivity
import com.jl.book.base.viewBinding
import com.jl.book.bean.Contacts
import com.jl.book.databinding.ActivityWanSearchBinding
import com.jl.book.db.ContactDao
import com.jl.book.db.ContactDataBase
import com.jl.book.global.TABLE_FOR_CONTACT
import com.jl.book.ui.base.ListItemActivity
import com.jl.book.util.drawShape
import com.thinkcool.circletextimageview.CircleTextImageView

/**
 *@author jl
 */
class SearchActivity : BaseActivity() {

    private val binding by viewBinding(ActivityWanSearchBinding::inflate)
    override fun getBindingRoot() = binding.root

    private lateinit var mAdapter :BaseQuickAdapter<Contacts,BaseViewHolder>

    private lateinit var mDao: ContactDao

    override fun initView() {

        mDao = Room.databaseBuilder(this, ContactDataBase::class.java, TABLE_FOR_CONTACT)
            .allowMainThreadQueries().build().contactDao()

        binding.searchRecycler.layoutManager = LinearLayoutManager(this)
        mAdapter = object :BaseQuickAdapter<Contacts,BaseViewHolder>(R.layout.adapter_contacts_item){
            override fun convert(holder: BaseViewHolder, item: Contacts) {
                val itemView = holder.itemView
                val position = getItemPosition(item)
                val mName: TextView = itemView.findViewById(R.id.mName)
                val mUserPhoto: CircleTextImageView = itemView.findViewById(R.id.mUserPhoto)
                val mBottomLayout: LinearLayout =
                    itemView.findViewById(R.id.mBottomLayout)
                //这里实现主界面点击联系人
                //这里实现主界面点击联系人
                itemView.setOnClickListener {
                    val id = item.id
                    val intent = Intent(this@SearchActivity, ListItemActivity::class.java)
                    intent.putExtra("id", id)
                    startActivity(intent)
                }
                if (position < data.size - 1) {
                    if (getItem(position).sortKey == getItem(position + 1).sortKey) {
                        mBottomLayout.visibility = View.GONE
                    } else {
                        mBottomLayout.visibility = View.VISIBLE
                    }
                } else {
                    mBottomLayout.visibility = View.GONE
                }
                val name = getItem(position).name
                try {
                    mName.text = name
                    if (name.endsWith("(") ||
                        name.endsWith(")") ||
                        name.endsWith("[") ||
                        name.endsWith("]") ||
                        name.endsWith("（") ||
                        name.endsWith("）") ||
                        name.endsWith("【") ||
                        name.endsWith("】")
                    ) {
                        mUserPhoto.setText(name.substring(name.length - 2, name.length - 1))
                    } else {
                        mUserPhoto.setText(name.substring(name.length - 1))
                    }
                } catch (e: Exception) {
                    mName.setText(R.string.unknown_name)
                    mName.setTextColor(resources.getColor(R.color.gray))
                    mUserPhoto.setText(R.string.unknown_contact_icon)
                }
            }
        }
        binding.searchRecycler.adapter = mAdapter

        //search
        binding.searchLayout.background =
            drawShape(this, R.dimen.corner_20, R.color.white)
        //back
        binding.searchBackIcon.setOnClickListener { finish() }

        binding.searchEdt.addTextChangedListener {
            if (it != null && it.isNotEmpty()) {
                mAdapter.setList(mDao.getFind(it.toString()))
            } else {
                mAdapter.setList(ArrayList())
            }
        }
    }

    override fun initData() {

    }
}